package general

import (
	"alex/ptp1588boundaryclock/datatypes"
)

type Follow_UpMessage struct {
	Timestamp *datatypes.TimeStamp
}

func (a* Follow_UpMessage) Init() {
	a.Timestamp = new(datatypes.TimeStamp)
}

// =====================================================================================================================
// Write Follow_Up Message
// =====================================================================================================================

// Writes the Follow_Up Message
func (a *Follow_UpMessage) Write(follow_up []byte, done chan bool) {
	//13.7.2.1 preciseOriginTimestamp (Timestamp)
	//9.5.10 The preciseOriginTimestamp field of the Follow_Up message shall be an estimate no worse than ±1 s of the <syncEventEgressTimestamp> of the associated Sync message excluding any fractional nanoseconds.
	for i := 0; i < 10; i++ {
		follow_up[i] = 0
	}
	done <- true
}

// =====================================================================================================================
// Read Follow_Up Message
// =====================================================================================================================

// Reads, checks and sets the Follow_Up Message values
func (a* Follow_UpMessage) Read(follow_up []byte) (check bool) {
	check = true
	// 13.7.2.1 preciseOriginTimestamp (Timestamp)
	a.readTimestamp(follow_up[:10])
	return
}

// Reads and sets the 13.7.2.1 preciseOriginTimestamp (Timestamp)
func (a* Follow_UpMessage) readTimestamp(timestamp []byte) {
	// we use uint64 since uint48 is not available in Go
	a.Timestamp.SecondsField = uint64(timestamp[0]) << 40 | uint64(timestamp[1]) << 32 | uint64(timestamp[2]) <<
			24 | uint64(timestamp[3]) << 16 | uint64(timestamp[4]) << 8 | uint64(timestamp[5])
	a.Timestamp.NanosecondsField = uint32(timestamp[6]) << 24 | uint32(timestamp[7]) << 16 | uint32(timestamp[8]) <<
			8 | uint32(timestamp[9])
}
